package com.weitu.threeSimultaneities.occHealth.controller;

import com.weitu.accessory.bean.AccessoryPackageInfo;
import com.weitu.accessory.service.AccessoryUtilService;
import com.weitu.accessory.util.AccessoryUtil;
import com.weitu.base.annotaion.SystemLog;
import com.weitu.base.controller.BaseController;
import com.weitu.threeSimultaneities.occHealth.entity.OccHealth;
import com.weitu.threeSimultaneities.occHealth.entity.OccHealthAccept;
import com.weitu.threeSimultaneities.occHealth.entity.OccHealthDesign;
import com.weitu.threeSimultaneities.occHealth.entity.OccHealthFsd;
import com.weitu.threeSimultaneities.occHealth.service.OccHealthAcceptService;
import com.weitu.threeSimultaneities.occHealth.service.OccHealthDesignService;
import com.weitu.threeSimultaneities.occHealth.service.OccHealthFsdService;
import com.weitu.threeSimultaneities.occHealth.service.OccHealthService;
import com.weitu.threeSimultaneities.projectApproval.entity.ProjectApproval;
import com.weitu.threeSimultaneities.projectApproval.entity.ProjectSpecialty;
import com.weitu.threeSimultaneities.projectApproval.service.ProjectApprovalService;
import com.weitu.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.weitu.util.AjaxResult.fail;
import static com.weitu.util.AjaxResult.success;

/**
 * Created by WT-SUN on 2017/5/25.
 */
@Controller
@RequestMapping(value = "threeSimultaneities/occHealth")
public class OccHealthController extends BaseController {
    @Autowired
    private AccessoryUtilService accessoryUtilService;

    @Autowired
    private OccHealthFsdService occHealthFsdService;

    @Autowired
    private OccHealthDesignService occHealthDesignService;

    @Autowired
    private OccHealthAcceptService occHealthAcceptService;

    @Autowired
    private OccHealthService  occHealthService;

    @Autowired
    private ProjectApprovalService projectApprovalService;

    /**
     * 打开编辑页面
     * @param model
     * @param id
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String editAjax(Model model, String id, String projectId,String type){

        //消防专业主数据
        OccHealth occHealth=occHealthService.selectByPrimaryKey(id);
        //主项目信息
        ProjectApproval projectApproval=projectApprovalService.selectByProjectId(projectId);
        //对项目专业ID进行拼接
        if(projectApproval.getProjectSpecialtyList().size()>0){
            String SpecialtyList="";
            for(ProjectSpecialty projectSpecialty:projectApproval.getProjectSpecialtyList()){
                SpecialtyList+=","+projectSpecialty.getSpecialtyId();
            }
            SpecialtyList=SpecialtyList.substring(1);
            model.addAttribute("SpecialtyList",SpecialtyList);
        }
        //项目主信息附件
        List<AccessoryPackageInfo> proAccessoryList = accessoryUtilService.queryAccessoryByBusinessId(projectApproval.getId().toString());
        //消防专业所有附件
        List<AccessoryPackageInfo> healthAccessoryList = accessoryUtilService.queryAccessoryByBusinessId(id);
        //可研阶段
        OccHealthFsd occHealthFsd=occHealthFsdService.selectBySubjectId(id);
        if(occHealthFsd!=null){ model.addAttribute("occHealthFsd", occHealthFsd);}
        //设计阶段
        OccHealthDesign occHealthDesign=occHealthDesignService.selectBySubjectId(id);
        if(occHealthDesign!=null){ model.addAttribute("occHealthDesign", occHealthDesign);}
        //验收阶段
        OccHealthAccept occHealthAccept=occHealthAcceptService.selectBySubjectId(id);
        if(occHealthAccept!=null){model.addAttribute("occHealthAccept",occHealthAccept);}

        model.addAttribute("occHealth", occHealth);
        model.addAttribute("healthAccessoryList", healthAccessoryList);
        model.addAttribute("proAccessoryList", proAccessoryList);
        model.addAttribute("projectApproval", projectApproval);
        return BASE_JSP_PATH + "threeSimultaneities/occHealth/"+type;
    }

    /**
     * 添加项可研阶段信息
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addOne", method = RequestMethod.POST)
    @Transactional(readOnly = false)
    @SystemLog(module = "职业卫生三同时", methods = "添加可研阶段信息")
    public AjaxResult insertSelective() {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        getAddBasicFieldMap(paramMap);
        paramMap.put("delState", 0);
        int boo = occHealthFsdService.insertSelective(paramMap);
        boolean accFlag = AccessoryUtil.saveAccerssoryInfo(paramMap.get("subjectId").toString());
        if (boo >0&& accFlag) {
            return success("添加成功");
        } else {
            return fail("添加失败");
        }
    }

    /**
     * 添加项设计阶段信息
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addTwo", method = RequestMethod.POST)
    @Transactional(readOnly = false)
    @SystemLog(module = "职业卫生三同时", methods = "添加设计阶段信息")
    public AjaxResult insertSelectivet() {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        getAddBasicFieldMap(paramMap);
        paramMap.put("delState", 0);
        int boo = occHealthDesignService.insertSelective(paramMap);
        boolean accFlag = AccessoryUtil.saveAccerssoryInfo(paramMap.get("subjectId").toString());
        if (boo >0&& accFlag) {
            return success("添加成功");
        } else {
            return fail("添加失败");
        }
    }

    /**
     * 添加项验收阶段信息
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addThree", method = RequestMethod.POST)
    @Transactional(readOnly = false)
    @SystemLog(module = "职业卫生三同时", methods = "添加验收阶段信息")
    public AjaxResult insertSelectiveh() {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        getAddBasicFieldMap(paramMap);
        paramMap.put("delState", 0);
        int boo = occHealthAcceptService.insertSelective(paramMap);
        boolean accFlag = AccessoryUtil.saveAccerssoryInfo(paramMap.get("subjectId").toString());
        if (boo >0&& accFlag) {
            return success("添加成功");
        } else {
            return fail("添加失败");
        }
    }
}
